import { RollupBabelInputPluginOptions } from '@rollup/plugin-babel'

import { getLoader } from '../utils'
import { BabelPresetOptions } from '../typing'

export const babelPlugin = function (options: BabelPresetOptions) {
    const { type = 'h5-lib' } = options

    const babel = getLoader<RollupBabelInputPluginOptions>('@rollup/plugin-babel')
    let runtimeType: 'bundled' | 'runtime' | 'inline' | 'external' = 'bundled'
    if (type.includes('lib')) {
        runtimeType = 'runtime'
    }
    return babel({
        babelrc: false,
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        exclude: '**/node_modules/**',
        babelHelpers: runtimeType,
        presets: [
            [
                require.resolve('babel-preset-uniify'),
                {
                    ...options
                }
            ]
        ]
    })
}
